package cn.wolfcode.car.business.web.controller;


import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.query.AppointmentQuery;

import cn.wolfcode.car.business.service.IAppointmentServicePlus;

import cn.wolfcode.car.business.service.IStatementServicePlus;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.web.AjaxResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.codec.json.AbstractJackson2Decoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * 服务单项控制器
 */
@Controller
@RequestMapping("business/appointment")
public class AppointmentController {
    //模板前缀
    private static final String prefix = "business/appointment/";

    @Autowired
    private IAppointmentServicePlus appointmentServicePlus;
    @Autowired
    private IStatementServicePlus statementServicePlus;


    //页面------------------------------------------------------------
    //列表
    @RequiresPermissions("business:appointment:view")
    @RequestMapping("/listPage")
    public String listPage(){
        return prefix + "list";
    }

    @RequiresPermissions("business:appointment:add")
    @RequestMapping("/addPage")
    public String addPage(){
        return prefix + "add";
    }


    @RequiresPermissions("business:appointment:edit")
    @RequestMapping("/editPage")
    public String editPage(Long id, Model model){
        model.addAttribute("appointment", appointmentServicePlus.getById(id));
        return prefix + "edit";
    }

    //数据-----------------------------------------------------------
    //列表
    @RequiresPermissions("business:appointment:list")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<Appointment> query(AppointmentQuery qo){
        return appointmentServicePlus.query(qo);
    }


    //新增
    @RequiresPermissions("business:appointment:add")
    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Appointment appointment){
        //由于页面没有给创建时间和状态添加对应输入框，因此需要手动设置默认值或手动进行赋值操作
        /*appointment.setCreateTime(new Date());
        appointment.setStatus(Appointment.STATUS_APPOINTMENT);*/
        UpdateWrapper<Appointment> wrapper = new UpdateWrapper<>();
        wrapper.set("status",Appointment.STATUS_APPOINTMENT);
        wrapper.set("create_time",new Date());
        appointmentServicePlus.save(appointment);
        return AjaxResult.success();
    }

    //编辑
    @RequiresPermissions("business:appointment:edit")
    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult edit(Appointment appointment){
            QueryWrapper<Appointment> wrapper = new QueryWrapper<>();
        /*//方式2：先查，在替换，最后再更新
        Appointment at = this.get(appointment.getId());
        //只有状态为预约状态才可以进行编辑
        if(!Appointment.STATUS_APPOINTMENT.equals(at.getStatus())){
            throw new BusinessException("只有是预约状态才可以进行编辑");
        }*/
        Appointment at = appointmentServicePlus.getById(appointment.getId());
        System.err.println(at);
        //只有状态为预约状态才可以进行编辑
        if(!Appointment.STATUS_APPOINTMENT.equals(at.getStatus())){
            throw new BusinessException("只有是预约状态才可以进行编辑");
        }
        wrapper.eq("id",appointment.getId());
        appointmentServicePlus.update(appointment,wrapper);
        return AjaxResult.success();
    }

    //删除
    /*@RequiresPermissions("business:appointment:remove")
    @RequestMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids){
        appointmentService.deleteBatch(ids);
        return AjaxResult.success();
    }*/

    @RequiresPermissions("business:appointment:cancel")
    @RequestMapping("/cancel")
    @ResponseBody
    public AjaxResult cancel(Long id){
        appointmentServicePlus.cancel(id);
        return AjaxResult.success();
    }

    /**
     * 软删除操作
     * @param id
     * @return
     */
    @RequiresPermissions("business:appointment:softRemove")
    @RequestMapping("/softRemove")
    @ResponseBody
    public AjaxResult softRemove(Long id){
        appointmentServicePlus.softRemove(id);
        return AjaxResult.success();
    }

    @RequiresPermissions("business:appointment:arrival")
    @RequestMapping("/arrival")
    @ResponseBody
    public AjaxResult arrival(Long id){
        appointmentServicePlus.arrival(id);
        return AjaxResult.success();
    }

    /**
     * 生成结算单
     */
    @RequiresPermissions("business:appointment:generateStatement")
    @RequestMapping("/generateStatement")
    @ResponseBody
    public AjaxResult generateStatement(Long appointmentId){
        Statement statement = statementServicePlus.generateStatement(appointmentId);
        //在页面中根据响应回的结算单ID跳转到明细编辑或者明细详情页面.
        return AjaxResult.success(statement.getId());
    }

}
